<?php

namespace app\index\controller;

use app\index\model\UserModel;
use think\Controller;
use think\Db;
use app\index\controller\Basecontroller;



class User extends Basecontroller
{
  protected $user = null;
  function __construct()
  {
    parent::__construct();
    $this->user =  new UserModel();
  }

  function userTopUp()
  {


    $post = getPost();
    $token = !empty(getheaders()['Access-Token']) ? getheaders()['Access-Token'] : null;
    if (!$token) {
      return res(1011, '服务器繁忙，请稍后重试');
    }


    $tokenData = json_decode($this->redis->get($token), true);
    if (!$tokenData) {
      return res(1011, '服务器繁忙，请稍后重试');
    }
    $res = $this->user->topUp($tokenData['uid'], $post['money']);

    if ($res === 1) {
    
      return res(1000, 'ok', $res);
    }
    return res(1040, '服务器繁忙');

  }

  function getOrders()
  {
    // return 'aa';
    $token = !empty(getheaders()['Access-Token']) ? getheaders()['Access-Token'] : null;
    if (!$token) {
      return res(1011, 'token错误');
    }
    $tokenData = json_decode($this->redis->get($token), true);
    $orders = $this->user->get_order($tokenData['uid']);
   
    return res(1000, 'ok' ,$orders);
  }

  function getMyGoods(){
    $token = !empty(getheaders()['Access-Token']) ? getheaders()['Access-Token'] : null;
    if (!$token) {
      return res(1011, '服务器繁忙，请稍后重试');
    }
    $tokenData = json_decode($this->redis->get($token), true);
    $goods= $this->user->get_myGoods($tokenData['uid']);

    return  res(1000,'ok',$goods);
  }

  function getCollectGoods(){
    
    $token = !empty(getheaders()['Access-Token']) ? getheaders()['Access-Token'] : null;
    if (!$token) {
      return res(1011, '服务器繁忙，请稍后重试');
    }
    $tokenData = json_decode($this->redis->get($token), true);
    $goods= $this->user->getCollectGoods($tokenData['uid']);

    return  res(1000,'ok',$goods);
  }
  function getHistory(){
    
    $token = !empty(getheaders()['Access-Token']) ? getheaders()['Access-Token'] : null;
    if (!$token) {
      return res(1011, '服务器繁忙，请稍后重试');
    }
    $tokenData = json_decode($this->redis->get($token), true);
    $goods= $this->user->getHistory($tokenData['uid']);

    return  res(1000,'ok',$goods);
  }

  function getUserChatLog(){
    $token = !empty(getheaders()['Access-Token']) ? getheaders()['Access-Token'] : null;
    if (!$token) {
      return res(1011, '服务器繁忙，请稍后重试');
    }
    $tokenData = json_decode($this->redis->get($token), true);



    $data= $this->user->userGetLog($tokenData['uid']);

    return  res(1000,'ok',$data);
    
  }

  function addHistory(){
    $post = getPost();
    $token = !empty(getheaders()['Access-Token']) ? getheaders()['Access-Token'] : null;
    if (!$token) {
      return res(1011, '服务器繁忙，请稍后重试');
    }
    $tokenData = json_decode($this->redis->get($token), true);



    $data= $this->user->addHistory($tokenData['uid'],$post['gid']);

    return  res(1000,'ok',$data);


  }

}
